package com.sskj.fca.ui.fragment;


import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.router.provider.LogoutProvider;
import com.sskj.fca.R;
import com.sskj.fca.presenter.BindMobileFragmentPresenter;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

@Route(path = RConfig.APP_FRAGMENT_BIND_MOBILE)
public class BindMobileFragment extends BaseFragment<BindMobileFragmentPresenter> {
    @BindView(R.id.et_new_tel)
    EditText etNewTel;
    @BindView(R.id.et_verification_code)
    EditText etVerificationCode;
    @BindView(R.id.et_money_pwd)
    EditText etMoneyPwd;
    @BindView(R.id.tvGetCode)
    TextView tvGetCode;
    @BindView(R.id.btn_save_tel)
    Button btnSaveTel;
    private DisposableSubscriber disposableSubscriber;

    @Override
    protected int getLayoutId() {
        return R.layout.app_fragment_bind_mobile;
    }

    @Override
    public BindMobileFragmentPresenter getPresenter() {
        return new BindMobileFragmentPresenter();
    }

    @Override
    public void initView() {
        ClickUtil.click(tvGetCode, () -> {
            if (TextUtils.isEmpty(etNewTel.getText().toString())) {
                ToastUtil.showShort("请输入手机号");
                return;
            }
            mPresenter.sendCode(etNewTel.getText().toString().trim());
        });
        ClickUtil.click(btnSaveTel, () -> {
            if (TextUtils.isEmpty(etNewTel.getText().toString())) {
                ToastUtil.showShort("请输入手机号");
                return;
            }
            if (!CheckUtil.isMobile(etNewTel.getText().toString())) {
                ToastUtil.showShort("请输入正确的手机号");
                return;
            }
            if (TextUtils.isEmpty(etVerificationCode.getText().toString())) {
                ToastUtil.showShort("请输入验证码");
                return;
            }
            if (TextUtils.isEmpty(etMoneyPwd.getText().toString())) {
                ToastUtil.showShort("请输入支付密码");
                return;
            }
            if (!CheckUtil.isPayPwd(etMoneyPwd.getText().toString())) {
                ToastUtil.showShort("密码格式错误");
                return;
            }
            mPresenter.updataTel(etVerificationCode.getText().toString().trim()
                    , etMoneyPwd.getText().toString().trim()
                    , etNewTel.getText().toString().trim());
        });
    }

    public void changeSuccess() {
        LogoutProvider logoutProvider = ARouter.getInstance().navigation(LogoutProvider.class);
        logoutProvider.logout();
    }

    public void sendCodeOk() {
        ToastUtil.showShort("验证码发送成功");
        startCount();
    }

    public void sendCodeError() {
        ToastUtil.showShort("验证码发送失败");
    }

    public void startCount() {
        tvGetCode.setEnabled(false);
        DisposUtil.close(disposableSubscriber);
        disposableSubscriber = new DisposableSubscriber<Long>() {
            @Override
            public void onNext(Long aLong) {
                int time = (int) (60 - aLong);
                tvGetCode.setText(String.format(App.INSTANCE.getString(R.string.app_count_down_register), time));
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.toString());
            }

            @Override
            public void onComplete() {
                tvGetCode.setText("再次获取验证码");
                tvGetCode.setEnabled(true);
                if (!disposableSubscriber.isDisposed()) {
                    disposableSubscriber.dispose();
                    disposableSubscriber = null;
                }

            }
        };
        etVerificationCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() == 6) {
                    btnSaveTel.setEnabled(true);
                } else {
                    btnSaveTel.setEnabled(false);
                }
            }
        });

        Flowable.interval(0, 1, TimeUnit.SECONDS, Schedulers.newThread())
                .take(60)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(disposableSubscriber);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        DisposUtil.close(disposableSubscriber);
    }
}
